#!/usr/bin/env python
#coding:utf-8

import thread, re, time, socket;
import getopt, shlex;
import datetime;
import sys;
from third.SSDB import SSDB
import comm.common_tools as commtools
#import third.SSDB as SSDB

reload(sys)
sys.setdefaultencoding('utf-8')


sdb_ip = '192.168.6.101'
sdb_port = 8222

#ddb_ip = '192.168.5.195'
ddb_ip = '192.168.5.56'
ddb_port = 32222

name_pre = 'MUSIC_LISTEN_COUNT_TYPE_1_'

err_stat = {'read':0, 'write': 0}

def ssdb_hmap_size(ssdb, name):
	ret = ssdb.request('hsize', [name]);
	if not ret.ok():
		return 0
	return ret.data


def ssdb_hmap_keys(ssdb, name, begin, end, limit):
	ret = ssdb.request('hkeys', [name, begin, end, limit]);
	if not ret.ok():
		return []
	return ret.data

def ssdb_hmap_get_value(ssdb, name, key):
	ret = ssdb.request('hget', [name, key]);
	if not ret.ok():
		return None
	return ret.data

def ssdb_hmap_set_value(ssdb, name, key, val):
	ret = ssdb.request('hset', [name, key, val]);
	if not ret.ok():
		return 0
	return 1


def process_ids(ssdb_r, ssdb_w, name, keys):
	global err_stat

	for id in keys:
		val = ssdb_hmap_get_value(ssdb_r, name, id)
		if None == val:
			err_stat['read'] += 1
			continue
		
		val = ssdb_hmap_set_value(ssdb_w, name, id, val)
		if 0 == val:
			err_stat['write'] += 1
			continue




def process_one_map(ssdb_r, ssdb_w, name):
	size = ssdb_hmap_size(ssdb_r, name);
	if 0 == size:
		print '\n表%s为空，无需迁移\n'

	key_begin = ''
	key_end = ''
	key_limit = 500
	keys = []


	processBar = commtools.ProgressBar(total=size, showtime=1)
	print '\n开始启动迁移ssdb表：%s, 记录数约: %d\n' % (name, size)

	gets = 0
	while 1:
		processBar.do_process(gets)
		keys = ssdb_hmap_keys(ssdb_r, name, key_begin, key_end, key_limit);
		#print keys;

		process_ids(ssdb_r, ssdb_w, name, keys)

		# 休眠200毫秒
		time.sleep(0.2)
		gets += len(keys)

		# 将当前的结尾，设置为下一个的起始
		if len(keys) == key_limit:
			key_begin = keys[ -1 ]
		else:
			break;
	processBar.do_process(gets)

	err_info = ', 成功率100%'
	if err_stat['read'] != 0 or err_stat['write'] != 0 :
		err_info= ', 其中读失败%d, 写失败%d' % (err_stat['read'], err_stat['write'])
		err_stat['read'] = 0
		err_stat['write'] = 0

	info = '\n迁移完成，共迁移%d条记录 %s\n\n' % (gets, err_info)
	print info


	#print gets


def main():
	ssdb_r = SSDB(sdb_ip, sdb_port)
	ssdb_w = SSDB(ddb_ip, ddb_port)


	for i in range(0,10):
		name = 'MUSIC_LISTEN_COUNT_TYPE_1_' + str(i)
		process_one_map(ssdb_r, ssdb_w, name)
		print 

if __name__ == '__main__':
	main()
	

'''
	LiangQuan, 2018/11/14,
	ssdb的迁移脚本，从sdb_ip将数据复制到ddb_ip中
	sdb_ip的数据以hmap方式存在，map-name规则如下，ddb中格式同sdb

	local table_n = tonumber(id) % 10
	local table_name = "MUSIC_LISTEN_COUNT_TYPE_" .. tostring(data_type) .. "_" .. table_n
	data_type = 1
'''
